var body = $('body');              
loading_obj = {};
loading_obj.isShow = 0;
// 顯示loading
$.show_loading = function () {
   if(typeof(overlay) != 'undefined'){
       if(overlay.hasClass('loading_overlay')){
           return;
       }
   }
    overlay = $('<div>');
    icon = $('<div>');
   overlay.addClass('loading_overlay');
   body.append(overlay);
   loading_obj.overlay = overlay;
   icon.addClass('loading_icon');
   body.append(icon);
   loading_obj.icon = icon;
   loading_obj.isShow = 1;
};

// 移除loading
$.hide_loading = function () {
   overlay.removeClass('loading_overlay');
   icon.removeClass('loading_icon');
   loading_obj.overlay.remove();
   loading_obj.icon.remove();
   loading_obj.isShow = 0;
};

$.isShow = function() {
    if (loading_obj.isShow == 1) {
        return true;
    }
    return false;
};

// 輸入限制plugin
$.fn.restrict = function (options) {
    var defaults = {
            reg: /^[a-zA-Z0-9]+$/
        };

    options = $.extend({}, defaults, options);

    return this.each(function () {
        var elem = $(this),
            reg = options.reg,
            flag = false,
            validation = function () {
                var str = '', i, val = elem.val();
                for (i = 0; i < val.length; i++) {
                    if (reg.test(str + val.charAt(i))) {
                        str += val.charAt(i);
                    }
                }
                elem.val(str);
            };

        elem.off('input').on('input', validation);

        if ($.msie) {
            elem.off('propertychange').on('propertychange', function () {
                // !flag 避免重複觸發
                if (!flag && window.event.propertyName.toLowerCase() === "value") {
                    flag = true;
                    validation();
                }
                flag = false;
            });
        }

    });
};

function show_loading(){
   $.show_loading();
}

// 顯示提示訊息
$.cust_noty = function (options) {
   var defaults = {
           type: 'info', // success、info、warning、danger
           message: '',
           delay: 3000
       },
       settings,
       div = $('<div>');

   settings = $.extend({}, defaults, options);

   div.attr({role: 'alert'}).addClass('alert').addClass('alert-' + settings.type).html(settings.message);

   div.css({position: 'fixed', top: '-9999em', width: '100%', textAlign: 'center', marginBottom: 0});
   body.append(div);

   div.css({display: 'none', top: 'auto', bottom: 0});
   div.slideDown();

   // callback往前移，顯示noty就執行
   if (settings.callback) {
       // noty完成彈出再執行callback
       setTimeout(function () {
           settings.callback();
       }, 500);
   }

   setTimeout(function () {
       div.slideUp(400, function () {
           div.remove();
       });
   }, settings.delay);
};